﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace BHair.Base
{
    public partial class frmjcgl : Form
    {
        public frmjcgl()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void button1_Click(object sender, EventArgs e)
        {
            jiazai();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Int32.Parse( dataGridView1.SelectedCells[3].Value.ToString()) <= 0)
            {
                string iid = dataGridView1.SelectedCells[2].Value.ToString();
                chuanzhi cz = new chuanzhi();
                cz.rizhi("删除卡" + textBox1.Text + "内项目，项目ID：" + iid);
                string sql = string.Format("DELETE FROM csmx where cardID='{0}' and jcspid='{1}' and 备注='{2}'", dataGridView1.SelectedCells[0].Value.ToString(), dataGridView1.SelectedCells[1].Value.ToString(), dataGridView1.SelectedCells[4].Value.ToString());
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                SqlCommand comm = new SqlCommand(sql, conn);
                comm.ExecuteNonQuery();
                MessageBox.Show("删除成功");
                jiazai();
                conn.Close();
            }
            else { MessageBox.Show("次数大于0不可删除"); }
        }
        public void jiazai()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = string.Format("SELECT csmx.cardID AS 卡号, csmx.jcspid AS 商品ID, items.iname AS 项目名称, csmx.jcnum AS 项目次数,备注 FROM csmx INNER JOIN items ON csmx.jcspid = items.iid WHERE (csmx.cardID = '{0}')", textBox1.Text);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            conn.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            chuanzhi cz = new chuanzhi();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                string sql =string.Format( "UPDATE csmx SET jcnum ={0} WHERE (cardID = '{1}') AND (jcspid = '{2}') and 备注='{3}'", this.dataGridView1.Rows[i].Cells[3].Value.ToString(),textBox1.Text, this.dataGridView1.Rows[i].Cells[1].Value.ToString(),this.dataGridView1.Rows[i].Cells[4].Value.ToString());
                cz.rizhi("修改卡内项目次数，卡号："+textBox1.Text+"项目ID"+ this.dataGridView1.Rows[i].Cells[1].Value.ToString()+"次数改为："+ this.dataGridView1.Rows[i].Cells[3].Value.ToString());
                SqlCommand comm = new SqlCommand(sql,conn);
                comm.ExecuteNonQuery();
            }
            MessageBox.Show("次数修改成功");
            jiazai();
        }
    }
}
